package com.casic.worklog.controller;

import com.casic.worklog.model.LogTable;
import com.casic.worklog.model.vo.Result;
import com.casic.worklog.service.LogTableService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 * @date 2019/9/14 23:49
 */
@RestController
@RequestMapping("/log")
public class LogTableController {

    @Resource
    private LogTableService logTableService;

    /**
     * 查询人员信息
     * @param page  当前页
     * @param limit 每页显示的条数
     * @return 附带分页信息的员工信息
     */
    @GetMapping("/findLogBy")
    public Result findLogBy (@RequestParam(defaultValue = "1") int page, @RequestParam
            (defaultValue = "10") int limit, Short id,LogTable logTable,String startDate,String endDate,String name) {
        logTable.setId(id);
        logTable.setName(name);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(startDate!=null&&startDate!=""){
                logTable.setStartDate(format.parse(startDate));
            }if(endDate!=null&&endDate!=""){
                logTable.setEndDate(format.parse(endDate));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Result result = logTableService.findLogBy(page, limit,logTable);
        return result;
    }

    /**添加或更新日志信息*/
    @RequestMapping("/saveLog")
    public Result save (LogTable logTable) {

        if (logTable.getLogId() == null) {
            //写日志时间
            logTable.setLogPletionTime(new Date());
            logTableService.insertLog(logTable);
            return Result.ok();
        } else {
            logTableService.updateLog(logTable);
            return Result.ok();
        }

    }
}
